package com.example.demo.controller;

import com.example.demo.commons.ApplicationVariable;
import com.example.demo.model.UserInfo;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

/**
 * @Author :蕉绿
 * @Date : 2023/2/21 21:51
 * @Version : V1.0
 * @Description :
 */
@Controller
@ResponseBody
@RequestMapping("/calc")
public class calcController {
    @RequestMapping("/add")
    public String calc(Integer num1,Integer num2) {
        return "结果: " + (num1 + num2);
    }

    @RequestMapping("/login")
    public String login(HttpServletRequest req, String name, String pwd) {
//        if (name != null && !name.equals("") &&
//                pwd != null && !pwd.equals("")
//                && pwd.equals("admin") && name.equals("admin")) {
//            // 登录成功
//            return "<h1>登录成功</h1>";
//        }
        if (StringUtils.hasLength(name) && StringUtils.hasLength(pwd) &&
                name.equals("admin") && pwd.equals("admin")) {
            // 登录成功
            HttpSession session = req.getSession(true);
            session.setAttribute(ApplicationVariable.USER_SESSION_KEY,
                    new UserInfo(1, "admin"));
            return "<h1>登录成功</h1>";
        }
        return "<h1>用户名或密码错误，请重试！</h1>";
    }
}
